package com.checkoutcheckins.server.auth;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;

public class OAuthLink extends HttpServlet {

	private static final long serialVersionUID = -9041977243989832355L;

	public void doGet(HttpServletRequest req, HttpServletResponse resp) {

		try {

			System.out.println("forward");

			CheckoutCheckinsOAuth oauth = new CheckoutCheckinsOAuth();

			String authUrl = oauth.getProvider().retrieveRequestToken(
					oauth.getConsumer(), "");

			HttpSession hs = req.getSession();
			hs.setMaxInactiveInterval(129600);
			hs.setAttribute("requestToken", oauth.getConsumer());

			resp.sendRedirect(authUrl);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OAuthMessageSignerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OAuthNotAuthorizedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OAuthExpectationFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OAuthCommunicationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
